package com.example.server

import com.example.server.db.tables.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.Before
import kotlin.test.Test

class ApplicationTest {

    @Before
    fun before() {
        println("========before=======")
        val driverClassName = "org.h2.Driver"
        // 嵌入db:file后面跟db绝对路径
        val jdbcURL = "jdbc:h2:file:./server/src/test/resources/db2;AUTO_SERVER=TRUE"
        val database = Database.connect(jdbcURL, driverClassName, )
        transaction(database) {
            addLogger(StdOutSqlLogger)

//            SchemaUtils.create(Articles, Users)
            SchemaUtils.createMissingTablesAndColumns(
                Articles,
                Users,
                AppVersions,
                Permissions,
                UserPermission,
                Roles,
                UserRole
            )
        }
    }

    @org.junit.Test
    fun testUserPerm() {
        runBlocking {
            newSuspendedTransaction(Dispatchers.IO) {
                val sql = UserPermission.leftJoin(Users)
                    .leftJoin(Permissions)
                    .selectAll()
                    .prepareSQL(QueryBuilder(false))

                println(sql)
            }
        }
    }

    @Test
    fun testSql() {
        runBlocking {
            newSuspendedTransaction(Dispatchers.IO) {
//                val sql = Articles.select(Op.nullOp()).prepareSQL(QueryBuilder(true))

                val sql = Articles.leftJoin(Users) //slice(Articles.userId, Users.id)
                    .selectAll().limit(1, 0)
                    .prepareSQL(QueryBuilder(false))
                println(sql)
            }
        }
    }
    @Test
    fun testRoot() {
        runBlocking {
            newSuspendedTransaction(Dispatchers.IO) {
//                val sql = Articles.select(Op.nullOp()).prepareSQL(QueryBuilder(true))

                val sql = Articles.leftJoin(Users) //slice(Articles.userId, Users.id)
                    .select {
                        Articles.id.eq(1)
                    }.limit(1, 0)
//                val sql =  Articles
//                    .select {
//                        Articles.id.eq(1L)
//                    }.limit(1, 0)
                    .prepareSQL(QueryBuilder(false))
                println(sql)
            }
        }
    }

    @Test
    fun testAlias() {
        runBlocking {
            newSuspendedTransaction(Dispatchers.IO) {
//                val sql = Articles.select(Op.nullOp()).prepareSQL(QueryBuilder(true))
//                val al = Articles.alias("a")
                val sql = Articles.leftJoin(Users) //slice(Articles.userId, Users.id)
                    .select {
                        Articles.id.eq(1)
                    }.limit(1, 0)
//                val sql =  Articles
//                    .select {
//                        Articles.id.eq(1L)
//                    }.limit(1, 0)
                    .prepareSQL(QueryBuilder(false))
                println(sql)
            }
        }
    }
}